/*获取商品列表*/

$.ajax({
    type: "post",
    url: url + "/shop/shoplist.php",
    headers: {
        'token': localStorage["token"]
    },
    data: {

    },
    dataType: "json",
    success: function (data) {
        console.log(data);
        $("#shopUl").empty();
        for (var i = 0 in data) {
            $("#shopUl").prepend("<li><img src=" + url + data[i].imagePath +
                " alt=''><h2>" + data[i].shopName + "</h2>"+(data[i].islimit==1?'<span>限购数量：'+data[i].limitNumber+'</span>':'')+" <span>商品状态："+(data[i].ShopState==1?'正常':'已下架')+"</span><span>库存：" + data[i]
                .shopNum + "</span><h3>单价：" + data[i].shopPrice +
                "￥</h3><div class='shop_thr_div'  data-id=" + data[i]._id + ">编辑</div><div class='shop_thr_div' data-id=" +
                data[i]._id + ">删除</div>")
        }
    }
});
/* 操作商品 */
$("#shopUl").click(function (even) {
    console.log(even.target.outerText);
    console.log(even);
    if (even.target.outerText == "删除") {
        console.log($(even.target).parent());
        $.ajax({
            type: "get",
            url: url + "/shop/del.php",
            data: {
                id: even.target.dataset.id,
            },
            dataType: "json",
            success: function (data) {
                console.log(data);
                $(even.target).parent().remove();
                hint(data.msg);
            }
        });
    } else if (even.target.outerText == "加入购物车") {

        $.ajax({
            type: "post",
            url: url + "/cart/joincart",
            data: {
                id: even.target.dataset.id,
            },
            dataType: "json",
            success: function (data) {
                console.log(data);
                console.log("加入购物车成功！");
                hint(data.msg);
                getGoodsNum();
            }
        });
    } else if (even.target.outerText == "编辑") {
        location.assign("upGoods.html#id=" + even.target.dataset.id);
    }
})